<template>
        <div class="page-100 login-register-part">
            <yd-cell-item style="padding-bottom: 10px">
                <input  slot="right" style="width: 100%;border: 0px solid #e6e6e6;" oninput="if (value.length>13)
                    value=value.slice(0,13)" min="11"  v-model="contact" placeholder="座机或手机号"  onKeypress="return (/[\d-]/.test(String.fromCharCode(event.keyCode)))">

            </yd-cell-item>

            <p class="destrice-text">座机请加上区号，如“010-81801970”。
                通过点点维获取服务时，服务商优先联系该号码。未填写联系电话，或联系电话无法联系到时，再通过手机号联系。</p>
            <yd-button size="large" type="primary" @click.native="reviseContactClick" style="background-color:#3cc8a8;color:white;height: 40px">确定</yd-button>

        </div>

</template>

<script>
    import {userInfoData} from '../../globalVariables'
    import {saveContactNumber} from "../../pcof/post";

    export default {
        name: "reviseContact",
        data() {
            return {
                contact:JSON.parse(localStorage.getItem(userInfoData)).contact_number,
            };
        },

        methods:{

            async reviseContactClick()
            {

                if(this.contact===''||this.contact===null)
                {
                    this.$dialog.toast({
                        mes: '请输入联系电话！',
                        timeout: 1500
                    });
                    return;
                }
                if(this.contact.length>13||this.contact.length<11)
                {
                    this.$dialog.toast({
                        mes: '格式错误！',
                        timeout: 1500
                    });
                    return;
                }
                if(JSON.parse(localStorage.getItem(userInfoData)).mobile ==this.contact)
                {
                    console.log(this.contact);
                    this.$dialog.toast({
                        mes: '不可与当前账号的登录手机号重复！',
                        timeout: 1500
                    });
                    return;
                }


                var num=0;
                if(this.contact.indexOf('-')!=-1)
                {
                    //不是固话
                    console.log('是固话');
                    for (var i=0;i<this.contact.length;i++)
                    {
                        var temp=this.contact.substring(i,i+1);
                        if (temp==='-')
                        {
                            num++;
                        }
                    }
                    if(num>1)
                    {
                        this.$dialog.toast({
                            mes: '格式错误！',
                            timeout: 1500
                        });
                        return;
                    }


                }else
                {
                    //手机号
                    console.log('不是固话');
                    var number= this.contact.substring(0,1);
                    if (number ==='1')
                    {
                        if(this.contact.length!=11)
                        {
                            this.$dialog.toast({
                                mes: '格式错误！',
                                timeout: 1500
                            });
                            return;
                        }

                    }else
                    {

                        this.$dialog.toast({
                            mes: '格式错误！',
                            timeout: 1500
                        });
                        return;

                    }

                }
                var  response = await  saveContactNumber(JSON.parse(localStorage.getItem(userInfoData)).mobile,this.contact);
                this.$dialog.loading.open('修改中...');
                console.log(response);
                if (response.data.code === 200) {
                    this.$dialog.loading.close();
                    var dic=new Array();
                    dic =JSON.parse(localStorage.getItem(userInfoData));
                    var  newdic={};
                    for (var key in dic) {
                        if (key ==='contact_number')
                        {
                            newdic[key]=this.contact;
                        }else
                        {
                            newdic[key] = dic[key];
                        }
                    }
                    localStorage.removeItem(userInfoData);
                    localStorage.setItem(userInfoData,JSON.stringify
                    (newdic));
                    this.$router.back(-1);
                    this.$dialog.toast({
                        mes: '修改成功！',
                        timeout: 1500
                    });

                }else
                {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes:response.data.msg,
                        timeout: 1500
                    });
                }
            }

        }

    }
</script>

<style lang="scss">
 @import "../login/login.scss";
    .destrice-text
    {
       text-align: left;
        font-size: 0.2rem;
        color: #858383;
    }
 .background
 {
     background-color:#f8f8f8;
     height: 100%;
 }

</style>